package com.app.shortvideo

import android.app.Activity
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.text.TextUtils
import android.util.Log
import androidx.lifecycle.MutableLiveData
import com.app.shortvideo.base.BaseActivity
import com.app.shortvideo.base.MyActivityManager
import com.app.shortvideo.config.Constant
import com.app.shortvideo.startup.FirstStartUp
import com.app.shortvideo.startup.SecondStartUp
import com.app.shortvideo.ui.SplashActivity
import com.app.shortvideo.utils.jump.JumpUtils
import com.app.library.base.LibApplication
import com.blankj.utilcode.util.ClipboardUtils
import com.rousetime.android_startup.StartupListener
import com.rousetime.android_startup.StartupManager
import com.rousetime.android_startup.model.CostTimesModel
import com.rousetime.android_startup.model.LoggerLevel
import com.rousetime.android_startup.model.StartupConfig
import dagger.hilt.android.HiltAndroidApp

@HiltAndroidApp
class MApplication : LibApplication() {

    companion object {

        lateinit var mInstance :MApplication
        val costTimesLiveData = MutableLiveData<List<CostTimesModel>>()
        var activityManager: MyActivityManager? = null
    }

    override fun onCreate() {
        super.onCreate()
        mApp = this
        mInstance = this
        activityManager = MyActivityManager()
        registerActivityLifecycleCallbacks(activityManager)
        registerActivityLifecycle()
        initStartUp()
    }

    fun getActivityManager(): MyActivityManager? {
        return activityManager
    }
    private fun initStartUp() {
        // init network manager.
        val config = StartupConfig.Builder()
            .setLoggerLevel(LoggerLevel.DEBUG)
            .setAwaitTimeout(12000L)
            .setListener(object : StartupListener {
                override fun onCompleted(
                    totalMainThreadCostTime: Long,
                    costTimesModels: List<CostTimesModel>
                ) {
                    // can to do cost time statistics.
                    costTimesLiveData.value = costTimesModels
                    Log.d("StartupTrack", "onCompleted: ${costTimesModels.size}")
                }
            })
            .build()

        StartupManager.Builder()
            .setConfig(config)
            .addStartup(FirstStartUp())
            .addStartup(SecondStartUp())
            .build(this)
            .start()
            .await()
    }

    /**
     * 得到当前activity
     */
    fun getCurrentActivity(): Activity? {
        return activityManager!!.getStack().lastElement()
    }

    /**
     * 得到指点的activity
     */
    fun getSpecifyActivity(c: Class<*>): Activity? {
        var a: Activity? = null
        for (activity in activityManager!!.getStack()) {
            if (activity.javaClass == c) {
                a = activity
            }
        }
        return a
    }

    private fun registerActivityLifecycle() {
        registerActivityLifecycleCallbacks(object : ActivityLifecycleCallbacks {
            override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) {
            }

            override fun onActivityStarted(activity: Activity) {
//                checkClipboardContent(activity)
            }

            private fun checkClipboardContent(activity: Activity) {
                if (activity is SplashActivity) {
                    return
                }
            }

            override fun onActivityResumed(activity: Activity) {
                if (activity is BaseActivity<*, *>){}
            }

            override fun onActivityPaused(activity: Activity) {
                if (activity is BaseActivity<*, *>){}
            }

            override fun onActivityStopped(activity: Activity) {}
            override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle) {}
            override fun onActivityDestroyed(activity: Activity) {
            }
        })
    }
}